﻿
//----------------------------------------------------------------------------------------------------------------upload



function sendFileToServer(formData, status, close) {
    var uploadURL = "/Upload/Upload";
    var extraData = {};
    var jqXHR = $.ajax({
        xhr: function () {
            var xhrobj = $.ajaxSettings.xhr();
            if (xhrobj.upload) {
                xhrobj.upload.addEventListener('progress', function (event) {
                    var percent = 0;
                    var position = event.loaded || event.position;
                    var total = event.total;
                    if (event.lengthComputable) {
                        percent = Math.ceil(position / total * 100);
                    }
                    status.setProgress(percent);
                }, false);
            }
            return xhrobj;
        },
        url: uploadURL,
        type: "POST",
        contentType: false,
        processData: false,
        cache: false,
        data: formData,
        success: function (data) {
            status.setProgress(100);            
            $.ajax({
                url: '/Home/Index/' + $('.apriboxparentfolder').last().attr("id"),
                dataType: 'html',
                success: function (data) {                    
                    $('#main').html(data);                    
                }
            });
        }
    });

    status.setAbort(jqXHR);
}

var rowCount = 0;
function createStatusbar(obj) {
    rowCount++;
    var row = "odd";
    if (rowCount % 2 == 0) row = "even";
    this.statusbar = $("<div class='statusbar " + row + "'></div>");
    this.filename = $("<div class='filename'></div>").appendTo(this.statusbar);
    this.size = $("<div class='filesize'></div>").appendTo(this.statusbar);
    this.progressBar = $("<div class='progressBar'><div></div></div>").appendTo(this.statusbar);
    this.abort = $("<div class='abort'></div>").appendTo(this.statusbar);

    obj.append(this.statusbar);

    this.setFileNameSize = function (name, size) {
        var sizeStr = "";
        var sizeKB = size / 1024;
        if (parseInt(sizeKB) > 1024) {
            var sizeMB = sizeKB / 1024;
            sizeStr = sizeMB.toFixed(2) + " MB";
        }
        else {
            sizeStr = sizeKB.toFixed(2) + " KB";
        }

        this.filename.html(name);
        this.size.html(sizeStr);
    }
    this.setProgress = function (progress) {
        var progressBarWidth = progress * this.progressBar.width() / 100;
        this.progressBar.find('div').animate({ width: progressBarWidth }, 10).html(progress + "% ");
        if (parseInt(progress) >= 100) {
            this.abort.hide();
        }
    }
    this.setAbort = function (jqxhr) {
        var sb = this.statusbar;
        this.abort.click(function () {
            jqxhr.abort();
            sb.hide();
        });
    }
}
function handleFileUpload(files) {
    for (var i = 0; i < files.length; i++) {



        var allow = $('#AllowedSize').attr("value");
        var currentfilesize = files[i].size / 1024 / 1024;
        
               
        if ((allow - currentfilesize) > 0) {

            if ($('.progressbar').is(':visible'))
                OnUp();
            else $('.progressbar').fadeIn();                    
        

        var fd = new FormData();
        fd.append('file', files[i]);
        fd.append('folderId', $('.apriboxparentfolder').last().attr('id'));

        var status = new createStatusbar($('.loadcontent'));
        status.setFileNameSize(files[i].name, files[i].size);

        if (i == files.length - 1)
            close = true;

        sendFileToServer(fd, status,close);           

        }
        else {
            SpaceEndidPopUp();

        }
    }
}

$('body').on('change', '#files', function (e) {
    
    
    if ($('#files')[0].files[0] != null) {

        
        e.preventDefault();

        handleFileUpload($('#files')[0].files);
    }
});

                


//------------------------------------------------------newfolder


    var newfolderpopup;

    $('.newfolder').click(function () {
        newfolderpopup = $('#newfolderpopup').bPopup({

            easing: 'easeOutBack',
            speed: 500,
            transition: 'slideDown',
        });

        $('#newfoldername input[type="text"]').focus();
        $('#newfoldername input[type="text"]').select();

        $('#newfoldername form').attr({
            'action': '/Create/CreateFolder?parentFolderId='
                + $('.apriboxparentfolder').last().attr("id")
        });


    });



    function OnSuccess(data) {
        $('#main').load('/Home/Index/' + data.Id);
        
    }

    function OnBegin() {
        newfolderpopup.close();
    }

    function closeNewFolder() {
        newfolderpopup.close();
    }




